/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { checkAndTriggerRefresh } from '@/components/_pages/red-point/_utils';
import request from '@/utils/request';
import { message } from 'antd';
import { isArray } from 'lodash';

interface ListParamsType {
  part: string;
  hidStatus?: string;
  phone?: string | undefined;
  end_time?: string;
  start_time?: string;
  page_num?: string | number;
}

interface ApproveTemplateType {
  state: string;
  ids: string;
  reject_reason?: string;
}
interface BlackType {
  tplId?: string;
  courierPhone?: string;
  cm_id?: string;
  kb_id?: string;
  reject_reason?: string;
}

export async function getTemplateAuditList(
  params: ListParamsType,
  fromRedDot?: boolean,
): Promise<any> {
  const part = params.part;
  const url = ['9', '10', '11'].includes(part)
    ? '/CustomerServiceV2/smsSign'
    : ['12', '13', '14'].includes(part)
    ? '/CustomerServiceV2/vipSmsTemplate'
    : '/CustomerServiceV2/smsTemplate';
  return request(url, {
    method: 'POST',
    data: params,
  })
    .then((respondse) => {
      const { code, data = {}, msg } = respondse;
      const { desc = [], total_records } = data;
      const newList = isArray(desc) ? desc : [];
      if (code == 0) {
        return {
          dataList: newList,
          pagination: {
            total: total_records,
            current: params.page_num,
          },
        };
      } else {
        message.error(msg);
        return {
          dataList: [],
          pagination: {},
        };
      }
    })
    .then(checkAndTriggerRefresh(getTemplateAuditList, fromRedDot));
}

export async function approveTemplate(params: ApproveTemplateType): Promise<any> {
  return request('/CustomerServiceV2/smsTemplateApproval', {
    method: 'POST',
    data: params,
  }).then(({ code, data, msg }) => {
    if (code != 0) {
      message.error(msg);
    }
    return {
      result: code == 0 && data,
    };
  });
}

export async function templateBlack(params: BlackType): Promise<any> {
  return request('/CustomerServiceV2/smsTemplateBlack', {
    method: 'POST',
    data: params,
  }).then(({ code, data, msg }) => {
    if (code != 0) {
      message.error(msg);
    }
    return {
      result: code == 0 && data,
    };
  });
}

export async function smsSignchangeStatus(params: any): Promise<any> {
  return request('/CustomerServiceV2/smsSignApproval', {
    method: 'POST',
    data: params,
  }).then(({ code, data, msg }) => {
    if (code != 0) {
      message.error(msg);
    }
    return {
      result: code == 0 && data,
    };
  });
}

export async function vipsmsTemplateApproval(params: any): Promise<any> {
  return request('/CustomerServiceV2/vipsmsTemplateApproval', {
    method: 'POST',
    data: params,
  }).then(({ code, data, msg }) => {
    if (code != 0) {
      message.error(msg);
    }
    return {
      result: code == 0 && data,
    };
  });
}
